<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                xmlns="http://www.w3.org/1999/xhtml" version="1.0">
    <xsl:output method="xml" 
                doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" 
                doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
                encoding="utf-8"
                indent="yes"/>
    
    <xsl:template match="/">
        <xsl:choose>
            <xsl:when test="count(//medium) = 0">
                <p class="warning">Databáze nemá žádné záznamy!</p>
            </xsl:when>
            <xsl:otherwise>
                <h3>Seznam kategorií</h3>
                <ul>
                    <xsl:apply-templates select="myVideoWeb/category" mode="categoryList"/>
                </ul>
                <xsl:apply-templates select="myVideoWeb/category" mode="tables"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <xsl:template match="category" mode="categoryList">      
        <li>
            <xsl:element name="a">
                <xsl:attribute name="href">#<xsl:value-of select="@id"/></xsl:attribute>
                <xsl:value-of select="@name"/>
            </xsl:element>
        </li>   
    </xsl:template>

    <xsl:template match="myVideoWeb/category" mode="tables">
        <h3> 
            <xsl:element name="a">
                <xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute>
                <xsl:value-of select="@name"/>
            </xsl:element>
        </h3>
        <xsl:choose>
            <xsl:when test="count(medium) = 0">
                <p><dfn>Tato kategorie je prázdná.</dfn></p>
            </xsl:when>
            <xsl:otherwise>
                <table class="media">
                    <thead>
                        <tr>
                            <xsl:apply-templates select="header/column"/>
                            <th></th>
                            <th></th>
                        </tr>
                    </thead>
                    <tbody>
                        <xsl:apply-templates select="medium"/>   
                    </tbody>         
                </table>
            </xsl:otherwise>
        </xsl:choose>
        
    </xsl:template>

    <xsl:template match="column">
        <th><xsl:value-of select="@name"/></th>
    </xsl:template>
    
    <xsl:template match="medium">
        <xsl:variable name="thisMedium" select="."/>
        <tr>
            <xsl:for-each select="./../header/column">
                <xsl:variable name="thisColumn" select="@name"/>
                <td>
                    <xsl:for-each select="$thisMedium/record">
                        <xsl:if test="$thisColumn = @column">
                            <xsl:value-of select="."/>
                        </xsl:if>
                    </xsl:for-each>
                </td>
            </xsl:for-each>
            <td>
                <div class="update">
                    <xsl:element name="a">
                        <xsl:attribute name="href">/pb138-videoweb-2013/action/Update.jsp?id=<xsl:value-of select="@id"/></xsl:attribute>
                        <xsl:attribute name="title">Upravit</xsl:attribute>
                        <img src="update-icon.png" alt="update icon"/>
                    </xsl:element>
                </div>
            </td>
            <td>
                <div class="delete">
                    <xsl:element name="a">
                        <xsl:attribute name="href">/pb138-videoweb-2013/action/Delete.jsp?id=<xsl:value-of select="@id"/></xsl:attribute>
                        <xsl:attribute name="title">Smazat</xsl:attribute>
                        <img src="delete-icon.png" alt="delete icon"/>
                    </xsl:element>
                </div>
            </td>
        </tr>
    </xsl:template>

</xsl:stylesheet>
